package com.peanut.mvc.congif;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

/**
 * @className: com.peanut.mvc.congif-> WebInit
 * @description  测试用java类代替web.xml，注解代替web.xml配置。(web工程的初始化类)
 * @author: chenhs
 * @createDate: 2021-09-09 22:31
 * @version: 1.0
 */
  //一个类继承了 这个抽象法法就表示这个类就等同于 web.xml。
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override //获取当前根配置，指定spring配置类
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    @Override //指定springmvc配置类
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    @Override //指定DispatcherServlet的映射规则  （url-pattern）
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    @Override //过滤器
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        filter.setForceResponseEncoding(true);
        HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
        return new Filter[]{filter,hiddenHttpMethodFilter};
    }
}
